package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.BeanUtils;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.domain.SysBanner;
import com.linq.cool.system.dto.SysBannerDTO;
import com.linq.cool.system.dto.SysBannerQueryDTO;
import com.linq.cool.system.service.SysBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/1/30 19:18
 * @Description:
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("banner")
public class SysBannerController extends BaseController {
    @Autowired
    private SysBannerService sysBannerService;

    @PreAuthorize(hasPerm = "sys:banner:list")
    @GetMapping("list")
    public Result<?> list(SysBannerQueryDTO dto) {
        IPage<SysBanner> page = sysBannerService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    @GetMapping(value = "{bannerId}")
    public Result<?> getInfo(@PathVariable("bannerId") Long bannerId) {
        return Result.ok(sysBannerService.getById(bannerId));
    }


    @PreAuthorize(hasPerm = "sys:banner:add")
    @Log(moduleTitle = "轮播图", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<?> add(@Validated @RequestBody SysBannerDTO dto) {
        SysBanner banner = new SysBanner();
        BeanUtils.copyProperties(dto, banner);
        banner.setCreator(SecurityUtils.getUserId());
        return this.toResult(sysBannerService.save(banner));
    }

    @PreAuthorize(hasPerm = "sys:banner:edit")
    @Log(moduleTitle = "轮播图", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<?> edit(@Validated @RequestBody SysBannerDTO dto) {
        SysBanner banner = new SysBanner();
        BeanUtils.copyProperties(dto, banner);
        banner.setGmtModified(new Date()).setModifier(SecurityUtils.getUserId());
        return this.toResult(sysBannerService.updateById(banner));
    }

    @PreAuthorize(hasPerm = "sys:banner:remove")
    @Log(moduleTitle = "轮播图", businessType = BusinessType.DELETE)
    @DeleteMapping("{bannerIds}")
    public Result<?> remove(@PathVariable("bannerIds") List<Long> bannerIds) {
        return this.toResult(sysBannerService.removeByIds(bannerIds));
    }


    @Log(moduleTitle = "轮播图", businessType = BusinessType.UPDATE)
    @PreAuthorize(hasPerm = "sys:banner:edit")
    @PutMapping("/changeStatus/{bannerId}/{status}")
    public Result<?> changeStatus(@PathVariable("bannerId") Long bannerId, @PathVariable("status") Integer status) {
        SysBanner banner = new SysBanner();
        banner.setId(bannerId).setStatus(status);
        banner.setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(sysBannerService.updateById(banner));
    }

    /**
     * 查询首页轮播图
     *
     * @return
     */
    @GetMapping("/home/list")
    public Result<?> list() {
        return Result.ok(sysBannerService.list(new LambdaQueryWrapper<SysBanner>()
                                                       .select(SysBanner::getId, SysBanner::getName, SysBanner::getPic, SysBanner::getUrl)
                                                       .eq(SysBanner::getIsDeleted, UserConstants.StateEnum.STATUS_NORMAL.getCode())
                                                       .eq(SysBanner::getStatus, UserConstants.StateEnum.STATUS_NORMAL.getCode())
                                                       .orderByAsc(SysBanner::getSort)));
    }

    /**
     * 轮播图点击数+1
     *
     * @return
     */
    @PutMapping("addClickCount/{bannerId}")
    public Result<?> addClickCount(@PathVariable("bannerId") Long bannerId) {
        // 先查询
        SysBanner banner = sysBannerService.getById(bannerId);
        if (null == banner) {
            return Result.fail("该轮播图不存在");
        }
        banner.setClickCount(banner.getClickCount() + 1);
        return this.toResult(sysBannerService.updateById(banner));
    }


}
